<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Borrow extends CI_Controller {
	
	function __construct(){
		parent::__construct();
	}
	
	/**
	 * 标列表
	 * @param $ftype 标类型
	 */
	function index($qtype = ''){
		//搜索参数
		$data['where']['qtype'] 		= $qtype;
		$data['page_size']  			= BORROW_LIST_PAGE_SIZE;
		$data['page_start'] 			= 0;
		$data['select'] 				= 1;
		$this->load->model('Borrow_model');
		$vdata = $this->Borrow_model->get_borrow_blist($data);
		//获取用户信息
		$this->load->model('User_model');
		$this->load->model('Credit_model');
		$borrow_list = array();
		foreach ($vdata['borrow_list'] as $borrow){
			if ($borrow['ftype']!=6){
				$user_id = $borrow['user_id'];
				$user_obj = $this->User_model->get_by_uid($user_id);
				$borrow['username'] = $user_obj['username'];
				$borrow['province'] = $user_obj['province'];
				$borrow['city'] 	= $user_obj['city'];
				$borrow['area'] 	= $user_obj['area'];
				$credit_obj = $this->Credit_model->get_row($user_id);
				$credit_obj['credit_value'] = isset($credit_obj) ? $credit_obj['credit_value'] : 0;
				$borrow['user_credit'] 		= $credit_obj['credit_value'];
				$borrow['user_credit_img'] 	= $this->Credit_model->get_pic($credit_obj['credit_value']);
				$borrow_list[] 		= $borrow;
			}
		}
		
		//获取地区信息
		$this->load->model('Area_model', 'area');
		$vdata['area_list'] 				= $this->area->get_list();
		$vdata['borrow_list']				= $borrow_list;
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type1');//标种
		$vdata['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//借款期限
		$vdata['borrow_day_time_limit'] 	= $this->Borrow_model->arrays('borrow_day_time_limit');//借款期限
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_valid_time'] 		= $this->Borrow_model->arrays('borrow_valid_time');//有效时间
		$this->load->view('mobile/borrow_list', $vdata);
	}
	
	function obj($id){
		$this->load->model('User_model');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
		
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		$vdata['borrow'] = $borrow_obj;
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		//用户信息
		$user_id 							= $borrow_obj['user_id'];
		$user_obj 							= $this->User_model->get_by_uid($user_id);
		$vdata['user_obj'] 					= $user_obj;
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type1');//标种
		$this->load->view('mobile/borrow_obj', $vdata);
	}
	
	function tender_list($id){
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$borrow_tender_list 			= $this->Borrow_model->get_borrow_tender(array('where'=>array('borrow_id'=>$id)));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] 		= $user_obj['username'];
			$borrow_tender_list1[] 		= $tender;
		}
		$vdata['borrow_tender_list'] = $borrow_tender_list1;
		$this->load->view('mobile/borrow_tender_list', $vdata);
	}
	
	
	function tender_view($id){
		$user = $this->session->userdata('ych_user');//获取用户session信息
		if($user == null){//如果未登录就跳到登录页面
			header('Location: /mobile/login.html');
		}
		$this->load->model('User_model');
		$this->load->model('User_info_model');
		$this->load->model('Area_model', 'area');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
		$this->load->model('Credit_model');
		$this->load->model('User_amount_model');
		$this->load->model('Attestation_model');
	
	
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		$user = $this->session->userdata('ych_user');
		$user_account = array();
		if($user == null){
			$user_account['total'] 		= 0;
			$user_account['use_money'] 	= 0;
		}else{
			$user_account 					= $this->Account_model->get_account($user['user_id']);
			if($user_account == false){
				$user_account['total'] 		= 0;
				$user_account['use_money'] 	= 0;
	
			}
		}
		$vdata['user_account']			= $user_account;
		$tuser_account = array();
		if($user == null){
			$tuser_account['paypassword'] = "";
		}else{
			$tuser_account 					= $this->User_model->get_by_uid($user['user_id']);
			if($tuser_account == false){
				$tuser_account['paypassword'] = "";
			}
		}
		$vdata['tuser_account']			= $tuser_account;
		$vdata['borrow'] = $borrow_obj;
		$this->load->view('mobile/borrow_tender', $vdata);
	}
	
	function lz_tender_view($id){
		$user = $this->session->userdata('ych_user');//获取用户session信息
		if($user == null){//如果未登录就跳到登录页面
			header('Location: /mobile/login/index/index.html');
		}
		$this->load->model('User_model');
		$this->load->model('Account_model');
		$this->load->model('Borrow_model');
	
	
		$data['select'] = 1;
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, $data);
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$user = $this->session->userdata('ych_user');
		$user_account = array();
		if($user == null){
			$user_account['total'] 		= 0;
			$user_account['use_money'] 	= 0;
		}else{
			$user_account 					= $this->Account_model->get_account($user['user_id']);
			if($user_account == false){
				$user_account['total'] 		= 0;
				$user_account['use_money'] 	= 0;
	
			}
		}
		$vdata['user_account']			= $user_account;
		$tuser_account = array();
		if($user == null){
			$tuser_account['paypassword'] = "";
		}else{
			$tuser_account 					= $this->User_model->get_by_uid($user['user_id']);
			if($tuser_account == false){
				$tuser_account['paypassword'] = "";
			}
		}
		$vdata['tuser_account']			= $tuser_account;
	
		//用户信息
		$user_id = $borrow_obj['user_id'];
		$user_obj = $this->User_model->get_by_uid($user_id);
		$vdata['borrow'] = $borrow_obj;
		$vdata['user_obj'] = $user_obj;
		$vdata['title'] 					= $borrow_obj['title'].' - '.CON_KEYWORDS;
		$this->load->view('mobile/borrow_lz_tender', $vdata);
	}
	
	
	/**
	 * 投标
	 */
	function tender(){
		$id 				= $this->input->post("id");
		$account 			= $this->input->post("account");
		$paypassword 		= $this->input->post("paypassword");
		$dxb_pwd 			= $this->input->post("dxb_pwd")?$this->input->post("dxb_pwd"):"";
		$user = $this->session->userdata('ych_user');
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');
		//获取标信息
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		//验证支付密码
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		//验证提交参数
		$ret['msg'] = '';
		if($pay_result == '' || $pay_result == 0){
			$ret['msg'] = "支付密码不正确";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
			
		}
		if(!$this->myformcheck->IsDouble($account)){
			$ret['msg'] = "投标金额不正确";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['fstatus'] != 1){
			$ret['msg'] = "此标不能投";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if((time()-$borrow_obj['verify_time']) > ($borrow_obj['valid_time']*3600*24)){
			$ret['msg'] = "此标已过期";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['account'] == $borrow_obj['account_yes']){
			$ret['msg'] = "此标已满";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['lowest_account'] > $account){
			$ret['msg'] = "请输入正确的金额";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if(($borrow_obj['most_account'] > 0) && ($account > $borrow_obj['most_account'])){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['is_dxb'] == 1 && $dxb_pwd != $borrow_obj['dxb_pwd']){
			$ret['msg'] = "定向密码不正确";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['user_id'] == $user['user_id']){
			$ret['msg'] = "不能投自己的标";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		$user_tender_all = $this->Borrow_model->get_borrow_tender(array('where'=>array('user_id'=>$user['user_id'],'borrow_id'=>$id)));
		$user_tender_account = 0;
		foreach($user_tender_all as $tender_obj){
			$user_tender_account += $tender_obj['account'];	
		}
		if(($borrow_obj['most_account'] > 0) && (($account+$user_tender_account) > $borrow_obj['most_account'])){
			$ret['msg'] = "投标金额大于最大投标金额";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		
		//封装参数
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];
		$data['borrow_user_id'] = $borrow_obj['user_id'];
		$data['borrow_id'] 	= $id;
		
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		
		$user_account = $this->Account_model->get_account($user['user_id']);
		if($account > $user_account['use_money']){
			$ret['msg'] = "您的余额不足,请先充值";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		
		//体验金检验
		$timeLimt = $borrow_obj['time_limit'];
		if ($timeLimt<12){
			$expMoney = $user_account['exp_money_12']+$user_account['exp_money_24'];
			if ($user_account['use_money']-$account<$expMoney){
				$ret['msg'] = "体验金只能用于投资相应标种";
				$ret['url_next'] = "/mobile/borrow/obj/$id.html";
				$this->load->view('mobile/msg',$ret);
				return;
			}
		}elseif ($timeLimt>12){
			$expMoney = $user_account['exp_money_12'];
			if ($user_account['use_money']-$account<$expMoney){
				$ret['msg'] = "体验金只能用于投资相应标种";
				$ret['url_next'] = "/mobile/borrow/obj/$id.html";
				$this->load->view('mobile/msg',$ret);
				return;
			}
		}
		
		$account = round($account, 2);
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		$data['account'] 	= $account;
		
		//投标
		$flag = $this->Borrow_model->borrow_tender($data);
		if($flag == 1){
			if($borrow_obj['ftype'] == 5){//是否秒标
				$borrow_obj1 = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
				if($borrow_obj1['account'] == $borrow_obj1['account_yes']){//是否满标
					//执行复审操作
					$data['id']					= $id;
			  		$data['ip']					= $this->input->ip_address();
			  		$data["fstatus"]			= 3;
			  		$data["repayment_remark"]	= "秒标自动复审";
			  		$data["repayment_user"]		= 1;
		  			$flag = $this->Borrow_model->borrow_review_true($data);
		  			
		  			//执行还款操作
		  			$repayments = $this->Borrow_model->get_repayment_list(array('select'=>1,'where'=>array('borrow_id'=>$id, 'fstatus'=>0)));
		  			foreach($repayments['repayment_list'] as $repayment){
		  				$data['repayment_id'] 	= $repayment['id'];
						$data['user_id'] 		= $repayment['user_id'];
						$data['ip']	 			= $this->input->ip_address();
						$this->Borrow_model->repay($data);
		  			}
				}
			}
			$ret['msg'] = "投标成功";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}else{
			$ret['msg'] = "投标失败";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
	}
	
	/**
	 * 流转标投标
	 */
	function lz_tender(){
		$id 				= $this->input->post("id");
		$money 				= $this->input->post("lz_account");
		$paypassword 		= $this->input->post("paypassword");
		$dxb_pwd 			= $this->input->post("dxb_pwd1")?$this->input->post("dxb_pwd1"):"";
		$user = $this->session->userdata('ych_user');
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('Account_model');
		$borrow_obj = $this->Borrow_model->get_borrow_obj($id, array('select'=>1));
		$pay_result = $this->User_model->check_paypassword($user['user_id'], $paypassword);
		if($pay_result == '' || $pay_result == 0){
			$ret['msg'] = "支付密码不正确";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if(!$this->myformcheck->isNumber($money)){
			$ret['msg'] = "份额不正确";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['account'] == $borrow_obj['account_yes']){
			$ret['msg'] = "此标已满";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['user_id'] == $user['user_id']){
			$ret['msg'] = "不能投自己的标";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		if($borrow_obj['is_dxb'] == 1 && $dxb_pwd != $borrow_obj['dxb_pwd']){
			$ret['msg'] = "定向密码不正确";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		$account = $money*$borrow_obj['lowest_account'];
		$data['id'] 		= $id;
		$data['money'] 		= $account;
		$data['ip'] 		= $this->input->ip_address();
		$data['user_id'] 	= $user['user_id'];
		$data['borrow_id'] 	= $id;
		
		if($borrow_obj['account'] - $borrow_obj['account_yes'] < $account){
			$account = $borrow_obj['account'] - $borrow_obj['account_yes'];
		}
		$user_account = $this->Account_model->get_account($user['user_id']);
		if($user_account['use_money'] < $account){
			$ret['msg'] = "您的余额不足，请先充值";
			$ret['url_next'] = "/mobile/borrow/obj/$id.html";
			$this->load->view('mobile/msg',$ret);
			return;
		}
		
		//体验金检验
		$timeLimt = $borrow_obj['time_limit'];
		if ($timeLimt<12){
			$expMoney = $user_account['exp_money_12']+$user_account['exp_money_24'];
			if ($user_account['use_money']-$account<$expMoney){
				$ret['msg'] = "体验金只能用于投资相应标种";
				$ret['url_next'] = "/mobile/borrow/obj/$id.html";
				$this->load->view('mobile/msg',$ret);
				return;
			}
		}elseif ($timeLimt>12){
			$expMoney = $user_account['exp_money_12'];
			if ($user_account['use_money']-$account<$expMoney){
				$ret['msg'] = "体验金只能用于投资相应标种";
				$ret['url_next'] = "/mobile/borrow/obj/$id.html";
				$this->load->view('mobile/msg',$ret);
				return;
			}
		}
		
		$account = max(0, $account);
		$account = min($account, $user_account['use_money']);
		$data['account'] 	= $account;
		
		$data['borrow_user_id'] = $borrow_obj['user_id'];
		
		$flag = $this->Borrow_model->borrow_lz_tender($data);
		$ret['msg'] = "投标成功";
		$ret['url_next'] = "/mobile/borrow/obj/$id.html";
		$this->load->view('mobile/msg',$ret);
		return;
	}
}